package org.study.shardingsphere.ds.controller;

import com.alibaba.fastjson2.JSON;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.study.shardingsphere.ds.model.TestModel;
import org.study.shardingsphere.ds.service.TestModelService;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2022-03-09
 */
@RestController
@RequestMapping("/dynamic")
public class TestModelController {
    @Resource
    private TestModelService testModelService;

    @RequestMapping("/insert")
    public String dynamicInsertTest() {
        for (int i = 0; i < 3; i++) {
            TestModel testModel = TestModel.builder()
                    .name("insert-sharding")
                    .createTime(new Date())
                    .updateTime(LocalDateTime.now())
                    .build();
            int result = testModelService.insert2Sharding(testModel);
            System.out.println(result);
        }
        for (int i = 0; i < 3; i++) {
            TestModel testModel = TestModel.builder()
                    .name("insert-master")
                    .createTime(new Date())
                    .updateTime(LocalDateTime.now())
                    .build();
            int result = testModelService.insert2Master(testModel);
            System.out.println(result);
        }
        for (int i = 0; i < 3; i++) {
            TestModel testModel = TestModel.builder()
                    .name("insert-slave")
                    .createTime(new Date())
                    .updateTime(LocalDateTime.now())
                    .build();
            int result = testModelService.insert2Slave(testModel);
            System.out.println(result);
        }
        return "插入成功";
    }

    @RequestMapping("/all")
    public List<TestModel> selectAll() {
        List<TestModel> list = testModelService.selectAll();
        list.forEach(item -> System.out.println(JSON.toJSONString(item)));
        return list;
    }
}
